package com.example.it;

import android.content.Intent;
import android.os.Build;
import com.google.android.material.tabs.TabLayout;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.example.it.View.TabView;
import com.example.it.adapter.VpFragmentAdapter;
import com.example.it.base.BaseActivity;
import com.example.it.bean.ChapterTabBean;
import com.example.it.bean.DataBean;
import com.example.it.fragments.ItInFragment;
import com.example.it.presenter.TabPresenter;

import java.util.ArrayList;

import butterknife.BindView;

public class MainActivity extends BaseActivity<TabPresenter> implements TabView {
    @BindView(R.id.main_bar)
    Toolbar Main1_bar;
    @BindView(R.id.tab)
    TabLayout mtab;
    @BindView(R.id.vp)
    ViewPager mvp;
    @BindView(R.id.main_iamge)
    ImageView image;
    private ArrayList<DataBean> dataBeans;
    private ArrayList<Fragment> fragments;

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        Intent intent = new Intent(this, Main2Activity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("data",dataBeans);
        intent.putExtras(bundle);
        startActivityForResult(intent,100);
    }
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void initView() {
        Main1_bar.setTitle("IT资询");
        dataBeans = new ArrayList<>();
        fragments = new ArrayList<>();
    }

    @Override
    protected void initPresenter() {
        mPresenter=new TabPresenter();
        mPresenter.getdata();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void onSuccess(ChapterTabBean data) {
        ArrayList<DataBean> data1 = data.getData();
        dataBeans.clear();
        dataBeans.addAll(data1);
        setVP();
    }

    private void setVP() {
        fragments.clear();
        for (int i = 0; i < dataBeans.size(); i++) {
            DataBean dataBean = dataBeans.get(i);
            if (dataBean.isSelect()) {
                ItInFragment inFragment = new ItInFragment();
               inFragment.getInstance(dataBean.getName(),dataBean.getId());
                Bundle bundle = new Bundle();
                bundle.putInt("id",dataBean.getId());
                inFragment.setArguments(bundle);
                fragments.add(inFragment);
            }
        }
        VpFragmentAdapter vpFragmentAdapter = new VpFragmentAdapter(getSupportFragmentManager(), fragments, dataBeans);
        mvp.setAdapter(vpFragmentAdapter);
        mtab.setupWithViewPager(mvp);
    }

    @Override
    public void onFali(String error) {
        Toast.makeText(this,error,Toast.LENGTH_LONG).show();
    }

    @Override
    public void showToast(String str) {
        Toast.makeText(this,str,Toast.LENGTH_LONG).show();

    }
}
